class ScanVulnerabilityWorker
  include Sidekiq::Worker
  sidekiq_options queue: "default"

  def perform(scan_id, options = {})
    scan = Scan.find(scan_id)
    options.symbolize_keys!

    vulnerabilities = API_Fuzzer.scan(options)
    vulnerabilities.each do |vuln|
      if vuln
        if vuln.instance_of? API_Fuzzer::Vulnerability
          scan.vulnerabilities.create!(
            status: vuln.type,
            description: vuln.description,
            value: vuln.value,
            class_type: vuln.class.to_s.split("::").last,
            parameter: vuln.parameter
          )
        else
          scan.vulnerabilities.create!(
            class_type: vuln.class.to_s.split("::").last,
            description: vuln.description,
            value: vuln.value,
            status: vuln.status
          )
        end
      end
    end
    scan.update_attributes(status: 'completed')
  rescue ActiveRecord::RecordNotFound => e
    Rails.logger.info e.message
  end
end
